import java.util.*;

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "val=" + val +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }
    }

    //以穷举的方式 创建一棵二叉树出来
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;

        return A;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;//空树是不需要遍历的
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    //求树的结点数
    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int tmp = size(root.left) + size(root.right) + 1;
        return tmp;
    }

    public int leafeNode;

    //得到叶子结点的个数
    public void getBinaryTreeLeafe1(TreeNode root) {
        if (root == null) {
            return;
        }

        if (root.left == null && root.right == null) {
            leafeNode++;
        }

        getBinaryTreeLeafe1(root.left);
        getBinaryTreeLeafe1(root.right);
    }

    public int getBinaryTreeLeafe2(TreeNode root) {
        if (root == null) {
            return 0;
        }

        if (root.left == null && root.right == null) {
            return 1;
        }

        return getBinaryTreeLeafe2(root.left) +
                getBinaryTreeLeafe2(root.right);
    }

    //root的第k层有多少个结点
    public int getBinaryTreeKNode(TreeNode root, int k) throws Exception {
        if (k < 0) {
            throw new Exception("k值由误");
        }
        if (root == null) {
            return 0;
        }
        if (root != null && k == 1) {
            return 1;
        }
        return getBinaryTreeKNode(root.left, k - 1) +
                getBinaryTreeKNode(root.right, k - 1);
    }

    //求树的高度
    public int getTreeHeight1(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int leftTree = getTreeHeight1(root.left);
        int rightTree = getTreeHeight1(root.right);

        return Math.max(leftTree, rightTree) + 1;
    }

    public int getTreeHeight2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftTree = getTreeHeight2(root.left);
        int rightTree = getTreeHeight2(root.right);

        return (leftTree > rightTree ? leftTree : rightTree) + 1;
    }

    //第三种：不推荐
    public int getTreeHeight3(TreeNode root) {
        if (root == null) {
            return 0;
        }

        return (getTreeHeight3(root.left) > getTreeHeight3(root.right) ?
                getTreeHeight3(root.left) : getTreeHeight3(root.right)) + 1;
    }

    //找结点
    public TreeNode find(TreeNode root, int val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode find1 = find(root.left, val);
        if (find1 != null) {
            return find1;
        }
        TreeNode find2 = find(root.right, val);
        if (find2 != null) {
            return find2;
        }
        return null;
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        if (root == null) {
            System.out.println("树为空");
            return;
        }
        Queue<TreeNode> nodeQueue = new LinkedList<>();
        nodeQueue.offer(root);

        while (!nodeQueue.isEmpty()) {
            TreeNode tmp = nodeQueue.poll();
            System.out.print(tmp.val + " ");
            TreeNode left = tmp.left;
            TreeNode right = tmp.right;
            if (left != null)
                nodeQueue.offer(left);
            if (right != null)
                nodeQueue.offer(right);
        }
    }

    // 判断一棵树是不是完全二叉树
    //第一种方法：保留所有的结点（包括空）如果按层次遍历在null后面还有非null结点，那么就不是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if (root == null) {
            System.out.println("树为空");
            return true;
        }
        //队列
        Queue<TreeNode> nodeQueue = new LinkedList<>();
        //顺序表
        List<TreeNode> nodeList = new ArrayList<>();
        nodeQueue.offer(root);
        //全部入队，然后保存起来
        int index = 0;
        while (!nodeQueue.isEmpty()) {
            TreeNode tmp = nodeQueue.poll();
            nodeList.add(tmp);
//            System.out.println(nodeList.get(index++));
            if (tmp != null) {
                TreeNode left = tmp.left;
                nodeQueue.offer(left);
            }
            if (tmp != null) {
                TreeNode right = tmp.right;
                nodeQueue.offer(right);
            }
        }
        //遍历顺序表，看是不是在两棵子树之间有null
        int isFlag = -1;
        for (int i = 0; i < nodeList.size(); i++) {
            //保留遇到第一个null的下标
            if (nodeList.get(i) == null) {
                isFlag = i;
                break;
            }
        }
        for (int i = isFlag; i < nodeList.size(); i++) {
            if (nodeList.get(i) != null)
                return false;
        }
        return true;
    }

    // 判断一棵树是不是完全二叉树
    //第二种方法：通过不饱和结点去判断
    /*
    不饱和结点包含了：（1）只有左子树；（2）只有右子树；（3）没有孩子结点
     */
    public boolean isCompleteTree1(TreeNode root) {
        if (root == null) {
            System.out.print("树为空：");
            return true;
        }
        //队列
        Queue<TreeNode> nodeQueue = new LinkedList<>();
        //顺序表
        nodeQueue.offer(root);
        boolean isLeafOrLeft = false;
        while (!nodeQueue.isEmpty()) {
            TreeNode poll = nodeQueue.poll();
            //得到第一个不饱和结点之后
            if (isLeafOrLeft) {
                //从第一个不饱和结点之后，所有结点都不能有孩子
                if (poll.left != null || poll.right != null) {
                    System.out.println("该二叉树不是完全二叉树");
                    return false;
                }
            }//没有找到不饱和结点就继续按照层次遍历寻找

            //poll结点的左右孩子都存在
            if (poll.left != null && poll.right != null) {
                nodeQueue.offer(poll.left);
                nodeQueue.offer(poll.right);
            } else if (poll.left != null) {
                //只有左孩子：找到不饱和结点
                nodeQueue.offer(poll.left);
                isLeafOrLeft = true;
            } else if (poll.right != null) {
                //只有右孩子：找到不饱和结点,该树不是完全二叉树
                nodeQueue.offer(poll.right);
                isLeafOrLeft = true;
//                System.out.println("该树不是完全二叉树");
//                return false;
            } else {
                //是叶子结点：找到不饱和结点
                isLeafOrLeft = true;
            }

        }
        System.out.println("该树是完全二叉树");
        return true;

    }

    /*
    判断一棵树是不是完全二叉树
    */
    public boolean isCompleteTree3(TreeNode root) {
        if (root == null) {
            System.out.println("树为空");
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur != null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else{
                break;
            }
        }
        //队列能将null放入
        //遍历剩下的元素
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur != null){
                return false;
            }
        }
        return true;
    }

    //找路径进栈
    public boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack) {
        if(root == null) {
            return false;
        }
        stack.push(root);
        if(root == node) {
            return true;
        }
        boolean flgLeft = getPath(root.left,node,stack);
        if(flgLeft) {
            return true;
        }
        boolean flgRight = getPath(root.right,node,stack);
        if(flgRight) {
            return true;
        }
        stack.pop();
        return false;
    }

    public TreeNode lowestCommonAncestor2(TreeNode root,
                                          TreeNode p, TreeNode q) {
        if(root == null){
            return null;
        }
        Stack<TreeNode> p1 = new Stack<>();
        Stack<TreeNode> q1 = new Stack<>();
        getPath(root,p,p1);
        getPath(root,q,q1);
        if(q1.size() > p1.size()){
            int count = q1.size() - p1.size();
            while(count != 0){
                q1.pop();
                count--;
            }
        }
        else if(p1.size() > q1.size()){
            int count = p1.size() - q1.size();
            while(count != 0){
                p1.pop();
                count--;
            }
        }
        while(p1.size() != 0){
            TreeNode tmpp = p1.pop();
            TreeNode tmpq = q1.pop();
            if(tmpq == tmpp)
                return tmpp;
        }
        return null;
    }
}
//    //时间复杂度O(min(m,n))
//    public boolean isSameTree(TreeNode node1,TreeNode node2){
//        //如果node1 == null，node2 != null 那就不试同一个结点，反之亦然
//        if(node1 == null && node2 != null || node1 != null && node2 == null){
//            return false;
//        }
//        //如果是都会空，那就是相同的树，这一般是遍历到叶子结点的下一层
//        if(node1 == null && node2 == null){
//            return true;
//        }
//        //如果node1和node2的val不相同，就不是同一颗树
//        if(node1.val != node2.val){
//            return false;
//        }
//        return isSameTree(node1.left,node2.left) && isSameTree(node1.right, node2.right);
//    }
//
//    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
//        if(root == null){
//            return false;
//        }
//        if(isSameTree(root,subRoot)){
//            return true;
//        }
//        if(isSubtree(root.left,subRoot)){
//            return true;
//        }
//        if(isSubtree(root.right,subRoot)){
//            return true;
//        }
//        return false;
//    }
//
//
//    //反转二叉树
//    public TreeNode invertTree(TreeNode root) {
//        if(root == null)
//            return null;
//        //如果是叶子结点就不交换
//        if(root.left == null && root.right == null)
//            return root;
//        TreeNode tmp = root.left;
//        root.left = root.right;
//        root.right = tmp;
//        invertTree(root.left);
//        invertTree(root.right);
//
//        return root;
//    }
